<?php
namespace backend\controllers;

use Yii;
use yii\web\Controller;
use common\models\LoginForm;
use common\models\Tag;
use backend\models\AccountForm;
use common\util\CommonUtil;
use common\util\FolderUtil;
use common\widgets\ajaxbtn\AjaxUtil;
use common\widgets\ajaxbtn\Status;

/**
 * Site controller
 */
class SiteController extends Controller
{

    /**
     * @inheritdoc
     */
    public function actions()
    {
        return [
            'error' => [
                'class' => 'yii\web\ErrorAction',
            ],
        ];
    }

    /**
     * Displays homepage.
     *
     * @return string
     */
    public function actionIndex()
    {
        $mysqlVersion = Yii::$app->db->pdo->getAttribute(\PDO::ATTR_SERVER_VERSION);
        return $this->render('index',[
            'mysql' => extension_loaded("mysql"),   //MYSQL组件是否加载
            'mysqlVersion' => $mysqlVersion,    //MYSQL版本
            'gd' => extension_loaded("gd"), //GD组件是否加载
            'curl' => extension_loaded("curl"), //CURL组件是否加载
            'intl' => extension_loaded("intl"), //INTL组件是否加载
            'serverIp' => CommonUtil::get_server_ip(),
            'garbageFileSize' => FolderUtil::setupSize(FolderUtil::dir_size("./upload/temp/"))
        ]);
    }
    
    public function actionClearTemp()
    {
        $forderPath = "./upload/temp";
        $fileList = CommonUtil::getFileList($forderPath);
        foreach ($fileList as $file){
            CommonUtil::deleteFile($file);
        }
        
        return $this->render('clear-temp');
    }
    
    public function actionTagSearch(){
    
        $keyword = get('term');
        $list = Tag::find()->where(['like', 'name', $keyword])->all();
    
        $arr = array();
        foreach ($list as $model){
    
            $obj = array();
            $obj['id'] = $model->id;
            $obj['label'] = $model->name;
            $obj['value'] = $model->name;
            $arr[] = $obj;
        }
    
        echo json_encode($arr);
    
    }
    
    public function actionAccount()
    {
        $user = Yii::$app->user->identity;
        $model = new AccountForm();
        $model->username = $user->username;
        $model->email = $user->email;
        if ($model->load($_POST) && $model->validate()) {
            $user->username = $model->username;
            $user->email = $model->email;
            if ($model->password) {
                $user->setPassword($model->password);
            }
            $user->save();
            
            AjaxUtil::returnBack(Status::$SUCCESS_ALERT_REFRESH,'操作成功！');
            
        }
        if($model->hasErrors()){
            AjaxUtil::returnBack(Status::$ERROR,$model->getErrors());
        }
        
        return $this->render('account', ['model'=>$model]);
    }
    
    /**
     * Login action.
     *
     * @return string
     */
    public function actionLogin()
    {
        if (!Yii::$app->user->isGuest) {
            return $this->goHome();
        }

        $model = new LoginForm();
        if ($model->load(Yii::$app->request->post()) && $model->login()) {
            return $this->goBack();
        } else {
            return $this->render('login', [
                'model' => $model,
            ]);
        }
    }

    /**
     * Logout action.
     *
     * @return string
     */
    public function actionLogout()
    {
        Yii::$app->user->logout();

        return $this->goHome();
    }
}
